<!doctype html>
<html lang="en" data-color-mode="dark">
<head>
<meta charset="utf-8">
<title>Flask 备忘清单
 &#x26;  flask cheatsheet &#x26;  Quick Reference</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta description="本清单对 Flask 的入门进行了简要的概述，以及其常用示例。需要有 HTML 和 Python 基础。

入门，为开发人员分享快速参考备忘单。">
<meta keywords="flask,reference,Quick,Reference,cheatsheet,cheat,sheet">
<link rel="icon" href="data:image/svg+xml,%3Csvg%20viewBox%3D%220%200%2024%2024%22%20fill%3D%22none%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20height%3D%221em%22%20width%3D%221em%22%3E%20%3Cpath%20d%3D%22m21.66%2010.44-.98%204.18c-.84%203.61-2.5%205.07-5.62%204.77-.5-.04-1.04-.13-1.62-.27l-1.68-.4c-4.17-.99-5.46-3.05-4.48-7.23l.98-4.19c.2-.85.44-1.59.74-2.2%201.17-2.42%203.16-3.07%206.5-2.28l1.67.39c4.19.98%205.47%203.05%204.49%207.23Z%22%20fill%3D%22%23c9d1d9%22%2F%3E%20%3Cpath%20d%3D%22M15.06%2019.39c-.62.42-1.4.77-2.35%201.08l-1.58.52c-3.97%201.28-6.06.21-7.35-3.76L2.5%2013.28c-1.28-3.97-.22-6.07%203.75-7.35l1.58-.52c.41-.13.8-.24%201.17-.31-.3.61-.54%201.35-.74%202.2l-.98%204.19c-.98%204.18.31%206.24%204.48%207.23l1.68.4c.58.14%201.12.23%201.62.27Zm2.43-8.88c-.06%200-.12-.01-.19-.02l-4.85-1.23a.75.75%200%200%201%20.37-1.45l4.85%201.23a.748.748%200%200%201-.18%201.47Z%22%20fill%3D%22%23228e6c%22%20%2F%3E%20%3Cpath%20d%3D%22M14.56%2013.89c-.06%200-.12-.01-.19-.02l-2.91-.74a.75.75%200%200%201%20.37-1.45l2.91.74c.4.1.64.51.54.91-.08.34-.38.56-.72.56Z%22%20fill%3D%22%23228e6c%22%20%2F%3E%20%3C%2Fsvg%3E" type="image/svg+xml">
<link rel="stylesheet" href="../style/style.css">
<link rel="stylesheet" href="../style/katex.css">
</head>
<body><nav class="header-nav"><div class="max-container"><a href="../index.html" class="logo"><svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" height="1em" width="1em">
  <path d="m21.66 10.44-.98 4.18c-.84 3.61-2.5 5.07-5.62 4.77-.5-.04-1.04-.13-1.62-.27l-1.68-.4c-4.17-.99-5.46-3.05-4.48-7.23l.98-4.19c.2-.85.44-1.59.74-2.2 1.17-2.42 3.16-3.07 6.5-2.28l1.67.39c4.19.98 5.47 3.05 4.49 7.23Z" fill="#c9d1d9"></path>
  <path d="M15.06 19.39c-.62.42-1.4.77-2.35 1.08l-1.58.52c-3.97 1.28-6.06.21-7.35-3.76L2.5 13.28c-1.28-3.97-.22-6.07 3.75-7.35l1.58-.52c.41-.13.8-.24 1.17-.31-.3.61-.54 1.35-.74 2.2l-.98 4.19c-.98 4.18.31 6.24 4.48 7.23l1.68.4c.58.14 1.12.23 1.62.27Zm2.43-8.88c-.06 0-.12-.01-.19-.02l-4.85-1.23a.75.75 0 0 1 .37-1.45l4.85 1.23a.748.748 0 0 1-.18 1.47Z" fill="#228e6c"></path>
  <path d="M14.56 13.89c-.06 0-.12-.01-.19-.02l-2.91-.74a.75.75 0 0 1 .37-1.45l2.91.74c.4.1.64.51.54.91-.08.34-.38.56-.72.56Z" fill="#228e6c"></path>
</svg>
<span class="title">Quick Reference</span></a><div class="menu"><a href="javascript:void(0);" class="searchbtn" id="searchbtn"><svg xmlns="http://www.w3.org/2000/svg" height="1em" width="1em" viewBox="0 0 18 18">
  <path fill="currentColor" d="M17.71,16.29 L14.31,12.9 C15.4069846,11.5024547 16.0022094,9.77665502 16,8 C16,3.581722 12.418278,0 8,0 C3.581722,0 0,3.581722 0,8 C0,12.418278 3.581722,16 8,16 C9.77665502,16.0022094 11.5024547,15.4069846 12.9,14.31 L16.29,17.71 C16.4777666,17.8993127 16.7333625,18.0057983 17,18.0057983 C17.2666375,18.0057983 17.5222334,17.8993127 17.71,17.71 C17.8993127,17.5222334 18.0057983,17.2666375 18.0057983,17 C18.0057983,16.7333625 17.8993127,16.4777666 17.71,16.29 Z M2,8 C2,4.6862915 4.6862915,2 8,2 C11.3137085,2 14,4.6862915 14,8 C14,11.3137085 11.3137085,14 8,14 C4.6862915,14 2,11.3137085 2,8 Z"></path>
</svg><span>搜索</span><span>⌘K</span></a><a href="https://github.com/jaywcjlove/reference/blob/main/docs/flask.md" class="" target="__blank"><svg viewBox="0 0 36 36" fill="currentColor" height="1em" width="1em"><path d="m33 6.4-3.7-3.7a1.71 1.71 0 0 0-2.36 0L23.65 6H6a2 2 0 0 0-2 2v22a2 2 0 0 0 2 2h22a2 2 0 0 0 2-2V11.76l3-3a1.67 1.67 0 0 0 0-2.36ZM18.83 20.13l-4.19.93 1-4.15 9.55-9.57 3.23 3.23ZM29.5 9.43 26.27 6.2l1.85-1.85 3.23 3.23Z"></path><path fill="none" d="M0 0h36v36H0z"></path></svg><span>编辑</span></a><button id="darkMode" type="button"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" class="light" height="1em" width="1em">
  <path d="M6.995 12c0 2.761 2.246 5.007 5.007 5.007s5.007-2.246 5.007-5.007-2.246-5.007-5.007-5.007S6.995 9.239 6.995 12zM11 19h2v3h-2zm0-17h2v3h-2zm-9 9h3v2H2zm17 0h3v2h-3zM5.637 19.778l-1.414-1.414 2.121-2.121 1.414 1.414zM16.242 6.344l2.122-2.122 1.414 1.414-2.122 2.122zM6.344 7.759 4.223 5.637l1.415-1.414 2.12 2.122zm13.434 10.605-1.414 1.414-2.122-2.122 1.414-1.414z"></path>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 24 24" class="dark" height="1em" width="1em">
  <path d="M12 11.807A9.002 9.002 0 0 1 10.049 2a9.942 9.942 0 0 0-5.12 2.735c-3.905 3.905-3.905 10.237 0 14.142 3.906 3.906 10.237 3.905 14.143 0a9.946 9.946 0 0 0 2.735-5.119A9.003 9.003 0 0 1 12 11.807z"></path>
</svg>
</button><script src="../js/dark.js?v=1.5.5"></script><a href="https://github.com/jaywcjlove/reference" class="" target="__blank"><svg viewBox="0 0 16 16" fill="currentColor" height="1em" width="1em"><path d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.012 8.012 0 0 0 16 8c0-4.42-3.58-8-8-8z"></path></svg></a></div></div></nav><div class="wrap h1body-exist max-container"><header class="wrap-header h1wrap"><h1 id="flask-备忘清单"><svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 24 24" height="1em" width="1em">
  <path d="M13.78 18.29c0 .12.15 0 0 0zm-3.35-2.7c.39.17-.31-.6 0 0zm-5.03.28c.15.26 0-.12 0 0zm5.6.39c-.27-.42-.2.06 0 .32.48.47.18-.26 0-.32z"></path>
  <path d="M20.24 19.06c-.53.63-1-.07-1.63.13-1.1 0 .09-.5.06-1-.58-.45-1.24.61-1.62.9a10.75 10.75 0 0 1-2.67-.83 8 8 0 0 1-2.6-1.94 8.36 8.36 0 0 1-1.87-2.84 21.36 21.36 0 0 1-.84-5.74c.07-1.3 1.44.14 2-.33-.21-.67-1.29-.88-1.89-1.16-.58 0-.79-.55-1.4-.59 0-.73-1.13-.33-1.56-.84.42-1.08-1.37 0-1.23-.91.58-1.2-1.78-1-.8.06.95.54-.93.93-.38 1.29a.59.59 0 0 1-.53.86c-.74.07-.67 1.09.06.55.81-.25.14.11-.25.18a1.3 1.3 0 0 0-.95 1.9 35.32 35.32 0 0 1 1 3.42A16.25 16.25 0 0 0 5.84 17 9.78 9.78 0 0 0 8 19a9.89 9.89 0 0 0 2.43 1.22 18 18 0 0 0 4.73.73 16.53 16.53 0 0 0 2.9-.24c.87.09 1.49-.65 2.26-.26.48-.42 1.16-.23 1.63-.54.32-1.22-1.22-.27-1.71-.85zm-1.74-.74c-.22 0 .05-.34 0 0zm-7.63-11c-.1 0-.08-.12-.16-.15-.8-.37.39-.23.16.19zm6.76 11.39c-.35-.08.28-.25 0 0zm-2.81.11c-.23 0 .18-.14 0 0zm-1.88-1.18c-.13 0 0-.13 0 0zm-.22-.17c-.36 0-.09-.23 0 0zm-.42-.36c-.33.08 0-.34 0 0zm-6-11.66c-.53-.28.11.33-.34.32-.24 0-.69-.52-.54-.78.3.16.83.21.92.5zm-.92-.81h-.11c-.03-.27.87-.06.15.04zm-1.02-.79c-.42-.19 0-.93.44-.58.51.73-.95-.08-.44.58zm.49.69c-.27.11.16-.21 0 0zm.48.92c.6.46-.34 0-.72.29-.61.06.66-.34.72-.29zm.17.6c.35 0 1.88-.69 1.37-.07.47.55.1.37-.21 0 .28.19.06 1-.37.45s-.5-.18-.17.21C5.8 6 4.88 6.28 4.21 6.5c-.43-.04 1.13-.37 1.29-.44zM3.58 7.51c-.16.4.24.53.62.6.62-.17-.41-.28.06-.44.26 0-.82-.26.08-.38S6.09 6.77 7 6.58a8.71 8.71 0 0 1 2.35-.19c.3.35-.23.94-.77 1a8.61 8.61 0 0 1-2 .39c-.8-.1-1.37.61-2.19.7.22-.4-.18-.14-.59-.16.21.82-1.47-.21-.48-.41-.74-.26-.75.52-.67 1-.65-.91.07-1.29.93-1.4zm1.88 1.08c-.22-.05.17-.2 0 0zm-.72.88s-.34-.08-.2-.17c1 .13-.9-.33.13-.22.66-.02.44.36.07.39zm.33.17c-.12.12 0-.24 0 0zm-.71 0c-.44-.09.21-.22 0 0zm-.06 1.43c-.07.1-.1-.08 0 0zM4 10.9c-.55-.19 0-.21 0 0zm-.12 1.28c-.26-.09-.25-.5-.37-.72-.38-1.03.41.4.33.72zm.23-1.49c-.69-.08-.05-.49 0 0zm0-.34h-.09c-.76-.35.45-.17-.02 0zM4 9.56c-.61-.31.58-.36 0 0zm.21.26c-.34.06.05-.32 0 0zm-1.3.07c-.12.08 0-.23 0 0zM3 9.75c-.16 0 0-.27 0 0zm.08-1c-.4-.1.33-.25 0 0zm.26 2c-.44-.1-.12-.65 0 0zm.3 1.25c.42.51.26.79.69 1.41-.56-.35-.67-1.2-1-1.75-.41-.9.23-.01.31.34zm4.09 6.38c-.34-.07-.57-.43-.81-.66a4.88 4.88 0 0 1-1.57-1.94 12.26 12.26 0 0 1-1.46-2.38c-.41-.92.25.07.35.42.21.68.88 1.07.94 1.52.74.42.36 1.12 1 1.31.27-.14.3.84.89.64-.33.15.13.33.3.61.35-.05 1.17.82.36.52zM4.62 12c-.31-.07.33-.11 0 0zm.2.47c-.55-.14.52-.2 0 0zm1.39.47c.4-.17.16.78.55.9.76 1-.53 0-1-.07.06 0-.38-.12-.19-.16-.14-.15-.45-.66.26-.32.6.24.65.06.38-.39zm1.31 1.76c0 .15-.72.13-.06.42-.38 0-.5 0 0 .3-.22.17.43.31.21.57.66.52-.12.13-.4-.08s-.85-.35-.75-.5c.81.42.21 0-.15-.3.65.16-.51-.48.1-.31.19.05-.27-.38.33-.07-.54-.52.47-.12.72-.07zm.48-.17c-.61-.14 0-.34 0 0zm-.17 2c-.61-.26.39 0 0 0zm-.45-.17c-.71-.23.52 0 0 0zm-.09.11c-.22 0 0-.17 0 0zm-1.08-1.55c-.72-.3-.2-.33 0 0zm-.54-.35c-.58-.33-.08-.33 0 0zm0 .65c-.57-.44-.07-.25 0 0zm-.15-1.34c-.35 0 .13-.2 0 0zM8 17.38c-.51-.15 0-.14 0 0zM7.63 17h-.16c-.68-.54.31-.38.75-.14.97.14-.22.39-.59.14zm2.7 1.65c-.68-.16.16-.27 0 0zm-.17-.52c-.05 0-.1-.11 0 0zm-.86-1.34c-.59-.14.21-.19.36 0 1.02.21-.06.26-.36 0zM9.51 18H9.4l-.13-.06c-.56-.59 1.19.28.24.06zm-.28.69c-.15.06 0-.14 0 0zm.29-1.19-.16-.05c-.66-.45.84.09.16 0zm.16 1.72c-.4-.16.26-.25 0 0zm.1-.74c-.58-.14.36-.13 0 0zm.13-.61c-.44-.06.05-.21 0 0zm-.72-.88c.46.06.47.27 0 .14-.09 0-.24 0-.28-.17-.76-.5.03-.25.28-.02zm-.06 2c-.2.12 0-.22 0 0zm-.31-.51c-.53 0 .4-.26 0 0zm-.12.35c-.26-.1.17-.08 0 0zm.3-.89c-.52-.14.25-.18 0 0zm-.16-.57c-.28 0-.06-.16 0 0zm-.54-.95c-.57-.46 1.18.38 0 0zm-.06.19c0 .06-.11-.09 0 0zm3.65 3.78a9.4 9.4 0 0 1-2.56-.85l-.33-.2c-.57-.31-.5-.38.06-.07.19.16 1.59.73.93.35-.29-.07-1.9-.95-.84-.57a20.92 20.92 0 0 0 2.74 1.21 7.14 7.14 0 0 0 2.43.15c.65.33 1.33-.08 2 .21a13.05 13.05 0 0 1-4.45-.23zm-1.25-.7c-.51-.09.16-.23 0 0zm.73-1c-.58-.11.6-.23 0 0zm.84 1.31c1 .37-.6.12-.93-.09-.92-.38.72.09.93.09zm-1.06-1.1c-.81-.12.9 0 0 0zm1.52.85c-.29.23 0-.3 0 0zm.29.05c-.15.06 0-.17 0 0zm.27-.38c-.16.2 0-.23 0 0zm.31 0c-.45.17.47-.39 0 0zm.14 0c.49-.42.51-.22.5.13-.43.48-.17-.2-.44.06s-.19-.1-.06-.17zm.83.45c-.5 0 .51-.54 0 0zm0 .18c-.2.05 0-.13 0 0zm-.16-.35c-.39-.26.94-.61 0 0zm.57.09h-.08c-.03-.23.54-.27.08.02zm.15.19c-.3-.08.47-.4 0 0zm.36-.1c-.51.18.48-.4 0 0zm.22.11-.12.08c-.23-.14.71-.48.12-.07zm-.43-.53c-.54-.18-1.4-.76-2-.37-.8.51.41-.56-.24-.08-.66.79-.45-.5-1.09-.11 0-.12-.57-.16-.17-.37-.36 0-.13-.05 0-.34-.16 0-1 0-1.36-.29-.4-.52.07-.3.48-.23-.67.12.24.5.68.29-.2-.09-.43-.52-1-.9a18.83 18.83 0 0 0-2.13-1c-.84-.5.18-.18.47.08.84.37.51-.51 0-.74-.54 0-.67-.59-1.1-.56.67.11 1.14-.17.39-.85s.3.63-.11.44c-.19-.25-.05-.6-.65-.73 0 .23-.08.58-.21.1-.17 0-.36.21-.5-.28.13-.57-.56-.7-.67-1.06.6.38.87.08.45-.33s-.33-.11-.49 0c-.59.6-.06-1-.32-.56-.26 0-.11.29-.1.65-.86-.21.2.45.45.81-.39-.4-.32.31-.65 0-.11-.3-.12-.77-.67-.85.62-.22-.36-.65-.28-.67.35-.32.4-.76.82-.07-.53 0 0 .88.12.6 0-.47.22-1-.59-1.19-.24.32-.65-.63-.18-.43-.06-.63.74-.26.69-.53.42-.18-.5-.77 0-1 .38.24 1.36-.36.7-.44.42 0 .75.18.79-.24.33.07 1.24.36.42-.09a3.45 3.45 0 0 1 1.13-.25c.79-.61.63 1 .45 1.45.3-.21.24.94.14 1.37.16-.22.23.36 0 .77 0-.07.4-.13.12.35-.22-.47-.31 1-.17.32.37-.61.28 0 .39.24.08.55.32 1.06.4 1.56.35.35-.15.11-.15.59.11-.14.58.19.57.68 0-.78.09-.18.47 0a5.48 5.48 0 0 0 1 1.42c.37.43 0 .13.14.39a3.19 3.19 0 0 0 2.09 1.28 1.46 1.46 0 0 0 1.57.7c-.6.37 1.68.47.91.67-.23.16-.62-.2-1.01-.18zM8.7 15.89c-.24 0 0-.2 0 0zm.38.22c-.18 0-.12-.24 0 0zm-2.61-3.65c-.24-.07.1-.09 0 0zm-.59 0c-.26 0 0-.34 0 0zm-.3-.08c-.1.05 0-.11 0 0zm11 8.25c-.39-.07.32-.22 0 0zm.73-1.05h-.3c.06-.45 1.33-.1 1.85-.09-.16.34-1.13.04-1.55.13zm2 0c-.27 0 .12-.32 0 0zm1-.09c-.25.13 0-.3 0 0zm.48.11c-.52-.12.24-.12.4-.14.94.02 0 .16-.47.16z"></path>
  <path d="M6.51 7.11c1-.2-.38-.33-.69 0 .18.13.41-.21.69 0zm-.82.13c0 .19.18-.22 0 0zm2.62-.48c.67-.11.11-.41-.09.1.07.14.11-.05.09-.1zm-.62.1c.93.36-.05-1 0 0zm-2.92.93a.51.51 0 0 1 .33.1c.75.52-.1-.84-.33-.1z"></path>
  <path d="M4.81 7.43c.16.13.29-.21 0 0zm.37.01.14.12c.68.06-.11-.46-.14-.12zM10.69 16c.09.3 0-.39 0 0zm-4.78-5c.1.18.18-.18 0 0zm.09.61c.19.34 0-.51 0 0zm.21-.49c.33.21-.28-.52 0 0zm-.41-.82c.07.15.24.16.37.2 1.32.61-.95-1-.37-.2zm.54 1.43c.31.53.18-.37 0 0zm.48-.16c.37.92-.23-.12-.3-.43a2.11 2.11 0 0 0 .28 1c.34.51.19-.28.55.23-.2-.45.1-.2.06-.4s-1-1.17-.59-.4zm.36 1.78c.09.31.18 0 0 0zM5.85 9c.06.14.11 0 0 0zm.67.42c.32.41.17-.36 0 0zm-.89 1.14.14.15c.99.29-1.05-.95-.14-.15zm.7-.37c1 .41-.09-.27-.44-.2.11.13.32.07.44.2zm-.18-.44c.18.18.82.51.31.06.32-.06 0-.16-.24-.32-.44-.67.07.06-.07.26z"></path>
  <path d="M5.51 10.64c-.53-.44.16.21-.06.2-.55-.4-.32-.11 0 .2.33.5.6-.3.06-.4zm2.14.64c.12.11-.06-.68 0 0zm1.02-3.5c0 .12.07-.1 0 0zM9 12.47c0 .21 0-.18 0 0zm-.29-1.22c.08.46.1-.78 0 0zm.14.22v.1c.21-.51.04-.79 0-.1zm-.1-2.7c-.14.7-.27.41-.08-.11-.05-.67-.45.41-.27.72.29.47.6-1.16.35-.61zm1.01 5.98c.13.35.11-.42 0 0zm-1.35-4.28c0 .2 0-.14 0 0zm.64 1.7c0 .26.11-.38 0 0zM9.5 14l.07.11c.14-.49-.36-.79-.07-.11zm-1.56-.52c.27.22-.13-.23 0 0zm1.23-.85c.08.29 0-.42 0 0zM8.14 9.81c.15.33-.05-.27 0 0z"></path>
  <path d="M8.3 10.23c.24.34-.26-.64 0 0zm-.06 1.15c-.13-1-.42.45 0 .69.13.34.25.2.3-.19-.32 0 .08-.62-.3-.5z"></path>
</svg>
<a aria-hidden="true" tabindex="-1" href="#flask-备忘清单"><span class="icon icon-link"></span></a>Flask 备忘清单</h1><div class="wrap-body">
<p>本清单对 Flask 的入门进行了简要的概述，以及其常用示例。需要有 <code>HTML</code> 和 <code>Python</code> 基础。</p>
</div></header><div class="menu-tocs"><div class="menu-btn"><svg aria-hidden="true" fill="currentColor" height="1em" width="1em" viewBox="0 0 16 16" version="1.1" data-view-component="true">
  <path fill-rule="evenodd" d="M2 4a1 1 0 100-2 1 1 0 000 2zm3.75-1.5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zm0 5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zm0 5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zM3 8a1 1 0 11-2 0 1 1 0 012 0zm-1 6a1 1 0 100-2 1 1 0 000 2z"></path>
</svg></div><div class="menu-modal"><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#入门">入门</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#相关链接">相关链接</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#hello-world">Hello World</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#hellopy">hello.py</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#运行-hellopy-程序">运行 hello.py 程序</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#或">或</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#html-转义">HTML 转义</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#路由">路由</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#变量规则">变量规则</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#唯一-url--重定向行为">唯一 URL / 重定向行为</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#url-建设">URL 建设</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#http-方法">HTTP 方法</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#blueprint">Blueprint</a></div></div><div class="h1wrap-body"><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="入门"><a aria-hidden="true" tabindex="-1" href="#入门"><span class="icon icon-link"></span></a>入门</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="相关链接"><a aria-hidden="true" tabindex="-1" href="#相关链接"><span class="icon icon-link"></span></a>相关链接</h3><div class="wrap-body">
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/HTML">HTML</a> <em>(developer.mozilla.org)</em></li>
<li><a href="https://www.python.org/">Python</a> <em>(python.org)</em></li>
<li><a href="https://flask.palletsprojects.com/">Flask</a> <em>(flask.palletsprojects.com)</em></li>
<li><a href="./python.html">Python 备忘清单</a> <em>(jaywcjlove.github.io)</em></li>
</ul>
</div></div></div><div class="wrap h3body-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="hello-world"><a aria-hidden="true" tabindex="-1" href="#hello-world"><span class="icon icon-link"></span></a>Hello World</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<h4 id="hellopy"><a aria-hidden="true" tabindex="-1" href="#hellopy"><span class="icon icon-link"></span></a>hello.py</h4>
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token comment"># 导入 Flask 类</span>
</span><span class="code-line"><span class="token keyword">from</span> flask <span class="token keyword">import</span> Flask
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 创建应用实例</span>
</span><span class="code-line">app <span class="token operator">=</span> Flask<span class="token punctuation">(</span>__name__<span class="token punctuation">)</span> <span class="token comment"># 'Flask' 参数是 应用程序模块 或 包 的名称</span>
</span><span class="code-line">                      <span class="token comment"># __name__是适用于大多数情况的便捷快捷方式</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 路由 (装饰器)</span>
</span><span class="code-line"><span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/'</span><span class="token punctuation">)</span>           <span class="token comment"># route()装饰器告诉 Flask 什么路径触发下面的功能</span>
</span><span class="code-line"><span class="token keyword">def</span> <span class="token function">hello</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">   <span class="token comment"># 该函数返回我们想要在浏览器中显示的消息内容</span>
</span><span class="code-line">   <span class="token keyword">return</span> <span class="token string">'Hello World!'</span>
</span><span class="code-line">   <span class="token comment"># 默认类型 HTML, 因此字符串中的 HTML 将被浏览器渲染</span>
</span><span class="code-line"><span class="token comment"># 启动服务</span>
</span><span class="code-line"><span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">'__main__'</span><span class="token punctuation">:</span>
</span><span class="code-line">   app<span class="token punctuation">.</span>run<span class="token punctuation">(</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="运行-hellopy-程序"><a aria-hidden="true" tabindex="-1" href="#运行-hellopy-程序"><span class="icon icon-link"></span></a>运行 <code>hello.py</code> 程序</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ python hello.py
</span><span class="code-line"> * Serving Flask app <span class="token string">'hello'</span>
</span><span class="code-line"> * Running on http://127.0.0.1:5000
</span><span class="code-line"> * Press CTRL+C to quit
</span></code></pre>
<h4 id="或"><a aria-hidden="true" tabindex="-1" href="#或"><span class="icon icon-link"></span></a>或</h4>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ flask <span class="token parameter variable">--app</span> hello run
</span><span class="code-line"> * Serving Flask app <span class="token string">'hello'</span>
</span><span class="code-line"> * Running on http://127.0.0.1:5000
</span><span class="code-line"> * Press CTRL+C to quit
</span><span class="code-line">$ flask run <span class="token parameter variable">--host</span><span class="token operator">=</span><span class="token number">0.0</span>.0.0
</span></code></pre>
<p>启用调试模式，使用 <code>--debug</code> 选项</p>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">$ flask <span class="token parameter variable">--app</span> hello <span class="token parameter variable">--debug</span> run
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="html-转义"><a aria-hidden="true" tabindex="-1" href="#html-转义"><span class="icon icon-link"></span></a>HTML 转义</h3><div class="wrap-body">
<pre class="language-py"><code class="language-py code-highlight"><span class="code-line"><span class="token keyword">from</span> markupsafe <span class="token keyword">import</span> escape
</span><span class="code-line">
</span><span class="code-line"><span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">"/&#x3C;name>"</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">def</span> <span class="token function">hello</span><span class="token punctuation">(</span>name<span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">return</span> <span class="token string-interpolation"><span class="token string">f"Hello, </span><span class="token interpolation"><span class="token punctuation">{</span>escape<span class="token punctuation">(</span>name<span class="token punctuation">)</span><span class="token punctuation">}</span></span><span class="token string">!"</span></span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="路由"><a aria-hidden="true" tabindex="-1" href="#路由"><span class="icon icon-link"></span></a>路由</h3><div class="wrap-body">
<pre class="language-py"><code class="language-py code-highlight"><span class="code-line"><span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/'</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">def</span> <span class="token function">index</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">return</span> <span class="token string">'Index Page'</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/hello'</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">def</span> <span class="token function">hello</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">return</span> <span class="token string">'Hello, World'</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="变量规则"><a aria-hidden="true" tabindex="-1" href="#变量规则"><span class="icon icon-link"></span></a>变量规则</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-py"><code class="language-py code-highlight"><span class="code-line"><span class="token keyword">from</span> markupsafe <span class="token keyword">import</span> escape
</span><span class="code-line">
</span><span class="code-line"><span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/user/&#x3C;username>'</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">def</span> <span class="token function">show_user_profile</span><span class="token punctuation">(</span>username<span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token comment"># 显示该用户的用户个人资料</span>
</span><span class="code-line">    <span class="token keyword">return</span> <span class="token string-interpolation"><span class="token string">f'User </span><span class="token interpolation"><span class="token punctuation">{</span>escape<span class="token punctuation">(</span>username<span class="token punctuation">)</span><span class="token punctuation">}</span></span><span class="token string">'</span></span>
</span><span class="code-line">
</span><span class="code-line"><span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/post/&#x3C;int:post_id>'</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">def</span> <span class="token function">show_post</span><span class="token punctuation">(</span>post_id<span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token comment"># 显示给定id的帖子，id是一个整数</span>
</span><span class="code-line">    <span class="token keyword">return</span> <span class="token string-interpolation"><span class="token string">f'Post </span><span class="token interpolation"><span class="token punctuation">{</span>post_id<span class="token punctuation">}</span></span><span class="token string">'</span></span>
</span><span class="code-line">
</span><span class="code-line"><span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/path/&#x3C;path:subpath>'</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">def</span> <span class="token function">show_subpath</span><span class="token punctuation">(</span>subpath<span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token comment"># 在 /path/ 之后显示子路径</span>
</span><span class="code-line">    <span class="token keyword">return</span> <span class="token string-interpolation"><span class="token string">f'Subpath </span><span class="token interpolation"><span class="token punctuation">{</span>escape<span class="token punctuation">(</span>subpath<span class="token punctuation">)</span><span class="token punctuation">}</span></span><span class="token string">'</span></span>
</span></code></pre>
<p>转换器类型：</p>





























<table><thead><tr><th align="left">:--</th><th>--</th></tr></thead><tbody><tr><td align="left"><code>string</code></td><td>（默认）接受任何没有斜杠的文本</td></tr><tr><td align="left"><code>int</code></td><td>接受正整数</td></tr><tr><td align="left"><code>float</code></td><td>接受正浮点值</td></tr><tr><td align="left"><code>path</code></td><td>像字符串但也接受斜线</td></tr><tr><td align="left"><code>uuid</code></td><td>接受 UUID 字符串</td></tr></tbody></table>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="唯一-url--重定向行为"><a aria-hidden="true" tabindex="-1" href="#唯一-url--重定向行为"><span class="icon icon-link"></span></a>唯一 URL / 重定向行为</h3><div class="wrap-body">
<pre class="language-py"><code class="language-py code-highlight"><span class="code-line"><span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/projects/'</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">def</span> <span class="token function">projects</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">return</span> <span class="token string">'The project page'</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/about'</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">def</span> <span class="token function">about</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">return</span> <span class="token string">'The about page'</span>
</span></code></pre>
<p>项目端点的规范 <code>URL</code> 有一个尾部斜杠。它类似于文件系统中的文件夹。如果您访问没有尾部斜杠 (<code>/projects</code>) 的 <code>URL</code>，<code>Flask</code> 会将您重定向到带有尾部斜杠 (<code>/projects/</code>) 的规范 <code>URL</code></p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="url-建设"><a aria-hidden="true" tabindex="-1" href="#url-建设"><span class="icon icon-link"></span></a>URL 建设</h3><div class="wrap-body">
<pre class="language-py"><code class="language-py code-highlight"><span class="code-line"><span class="token keyword">from</span> flask <span class="token keyword">import</span> url_for
</span><span class="code-line">
</span><span class="code-line"><span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/'</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">def</span> <span class="token function">index</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">return</span> <span class="token string">'index'</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/login'</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">def</span> <span class="token function">login</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">return</span> <span class="token string">'login'</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/user/&#x3C;username>'</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">def</span> <span class="token function">profile</span><span class="token punctuation">(</span>username<span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">return</span> <span class="token string-interpolation"><span class="token string">f'</span><span class="token interpolation"><span class="token punctuation">{</span>username<span class="token punctuation">}</span></span><span class="token string">\'s profile'</span></span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">with</span> app<span class="token punctuation">.</span>test_request_context<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">print</span><span class="token punctuation">(</span>url_for<span class="token punctuation">(</span><span class="token string">'index'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</span><span class="code-line">    <span class="token keyword">print</span><span class="token punctuation">(</span>url_for<span class="token punctuation">(</span><span class="token string">'login'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</span><span class="code-line">    <span class="token keyword">print</span><span class="token punctuation">(</span>url_for<span class="token punctuation">(</span><span class="token string">'login'</span><span class="token punctuation">,</span> <span class="token builtin">next</span><span class="token operator">=</span><span class="token string">'/'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</span><span class="code-line">    <span class="token keyword">print</span><span class="token punctuation">(</span>url_for<span class="token punctuation">(</span><span class="token string">'profile'</span><span class="token punctuation">,</span> username<span class="token operator">=</span><span class="token string">'John Doe'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="http-方法"><a aria-hidden="true" tabindex="-1" href="#http-方法"><span class="icon icon-link"></span></a>HTTP 方法</h3><div class="wrap-body">
<p>默认路由仅响应 <code>GET</code> 请求。可以使用 <code>route()</code> 装饰器的方法参数来处理不同的 <code>HTTP</code> 方法</p>
<pre class="language-py"><code class="language-py code-highlight"><span class="code-line"><span class="token keyword">from</span> flask <span class="token keyword">import</span> request
</span><span class="code-line">
</span><span class="code-line"><span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/login'</span><span class="token punctuation">,</span>methods<span class="token operator">=</span><span class="token punctuation">[</span><span class="token string">'GET'</span><span class="token punctuation">,</span><span class="token string">'POST'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">def</span> <span class="token function">login</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">if</span> request<span class="token punctuation">.</span>method <span class="token operator">==</span> <span class="token string">'POST'</span><span class="token punctuation">:</span>
</span><span class="code-line">        <span class="token keyword">return</span> do_the_login<span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">    <span class="token keyword">else</span><span class="token punctuation">:</span>
</span><span class="code-line">        <span class="token keyword">return</span> show_the_login_form<span class="token punctuation">(</span><span class="token punctuation">)</span>
</span></code></pre>
<p>您还可以将不同方法的视图分成不同的函数。 <code>Flask</code> 为每个常见的 <code>HTTP</code> 方法提供了一种快捷方式，用于使用 <code>get()</code>、<code>post()</code> 等来装饰此类路由</p>
<pre class="language-py"><code class="language-py code-highlight"><span class="code-line"><span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>get</span><span class="token punctuation">(</span><span class="token string">'/login'</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">def</span> <span class="token function">login_get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">return</span> show_the_login_form<span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>post</span><span class="token punctuation">(</span><span class="token string">'/login'</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">def</span> <span class="token function">login_post</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">return</span> do_the_login<span class="token punctuation">(</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="blueprint"><a aria-hidden="true" tabindex="-1" href="#blueprint"><span class="icon icon-link"></span></a>Blueprint</h3><div class="wrap-body">
<p>创建蓝图Bp1</p>
<pre class="language-py"><code class="language-py code-highlight"><span class="code-line"><span class="token keyword">from</span> flask <span class="token keyword">import</span> Blueprint<span class="token punctuation">,</span> abort<span class="token punctuation">,</span> jsonify
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 定义Bp1，并定义url前缀为/img</span>
</span><span class="code-line">Bp1 <span class="token operator">=</span> Blueprint<span class="token punctuation">(</span><span class="token string">'imgBlue'</span><span class="token punctuation">,</span> __name__<span class="token punctuation">,</span> template_folder<span class="token operator">=</span><span class="token string">'templates'</span><span class="token punctuation">,</span> url_prefix<span class="token operator">=</span><span class="token string">'/img'</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line">
</span><span class="code-line"><span class="token decorator annotation punctuation">@Bp1<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/getimg'</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">def</span> <span class="token function">getImg</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">try</span><span class="token punctuation">:</span>
</span><span class="code-line">        <span class="token keyword">return</span> jsonify<span class="token punctuation">(</span>name<span class="token operator">=</span><span class="token string">"img"</span><span class="token punctuation">,</span> size<span class="token operator">=</span><span class="token string">"100KB"</span><span class="token punctuation">)</span>
</span><span class="code-line">    <span class="token keyword">except</span> Exception <span class="token keyword">as</span> e<span class="token punctuation">:</span>
</span><span class="code-line">        abort<span class="token punctuation">(</span>e<span class="token punctuation">)</span>
</span></code></pre>
<p>创建蓝图Bp2</p>
<pre class="language-py"><code class="language-py code-highlight"><span class="code-line"><span class="token keyword">from</span> flask <span class="token keyword">import</span> Blueprint<span class="token punctuation">,</span> abort<span class="token punctuation">,</span> jsonify
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 定义Bp2，并定义url前缀为/video</span>
</span><span class="code-line">Bp2 <span class="token operator">=</span> Blueprint<span class="token punctuation">(</span><span class="token string">'videoBlue'</span><span class="token punctuation">,</span> __name__<span class="token punctuation">,</span> template_folder<span class="token operator">=</span><span class="token string">'templates'</span><span class="token punctuation">,</span> url_prefix<span class="token operator">=</span><span class="token string">'/video'</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line">
</span><span class="code-line"><span class="token decorator annotation punctuation">@Bp2<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/getvideo'</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">def</span> <span class="token function">getvideo</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">try</span><span class="token punctuation">:</span>
</span><span class="code-line">        <span class="token keyword">return</span> jsonify<span class="token punctuation">(</span>name<span class="token operator">=</span><span class="token string">"video"</span><span class="token punctuation">,</span> size<span class="token operator">=</span><span class="token string">"100GB"</span><span class="token punctuation">)</span>
</span><span class="code-line">    <span class="token keyword">except</span> Exception <span class="token keyword">as</span> e<span class="token punctuation">:</span>
</span><span class="code-line">        abort<span class="token punctuation">(</span>e<span class="token punctuation">)</span>
</span></code></pre>
<p>在flask app中引用蓝图Bp1和Bp2</p>
<pre class="language-py"><code class="language-py code-highlight"><span class="code-line"><span class="token keyword">from</span> flask <span class="token keyword">import</span> Flask<span class="token punctuation">,</span> jsonify
</span><span class="code-line"><span class="token keyword">from</span> lantu<span class="token punctuation">.</span>img <span class="token keyword">import</span> Bp1
</span><span class="code-line"><span class="token keyword">from</span> lantu<span class="token punctuation">.</span>video <span class="token keyword">import</span> Bp2
</span><span class="code-line">
</span><span class="code-line">app <span class="token operator">=</span> Flask<span class="token punctuation">(</span>__name__<span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 注册蓝图到app</span>
</span><span class="code-line">app<span class="token punctuation">.</span>register_blueprint<span class="token punctuation">(</span>Bp1<span class="token punctuation">)</span>
</span><span class="code-line">app<span class="token punctuation">.</span>register_blueprint<span class="token punctuation">(</span>Bp2<span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line">
</span><span class="code-line"><span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/'</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">def</span> <span class="token function">index</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">return</span> jsonify<span class="token punctuation">(</span>name<span class="token operator">=</span><span class="token string">'phyger'</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">'__main__'</span><span class="token punctuation">:</span>
</span><span class="code-line">    app<span class="token punctuation">.</span>run<span class="token punctuation">(</span>host<span class="token operator">=</span><span class="token string">"127.0.0.1"</span><span class="token punctuation">,</span> debug<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
</span></code></pre>
<p>简单测试</p>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token function">curl</span> http://127.0.0.1:5000/
</span><span class="code-line"><span class="token operator">>></span> <span class="token punctuation">{</span><span class="token string">"name"</span><span class="token builtin class-name">:</span><span class="token string">"phyger"</span><span class="token punctuation">}</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token function">curl</span> http://127.0.0.1:5000/img/getimg
</span><span class="code-line"><span class="token operator">>></span> <span class="token punctuation">{</span><span class="token string">"name"</span><span class="token builtin class-name">:</span> <span class="token string">"img"</span>, <span class="token string">"size"</span><span class="token builtin class-name">:</span> <span class="token string">"100KB"</span><span class="token punctuation">}</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token function">curl</span> http://127.0.0.1:5000/video/getvideo
</span><span class="code-line"><span class="token operator">>></span> <span class="token punctuation">{</span><span class="token string">"name"</span><span class="token builtin class-name">:</span> <span class="token string">"video"</span>, <span class="token string">"size"</span><span class="token builtin class-name">:</span> <span class="token string">"100GB"</span><span class="token punctuation">}</span>
</span></code></pre>
</div></div></div></div></div></div><script src="https://giscus.app/client.js" data-repo="jaywcjlove/reference" data-repo-id="R_kgDOID2-Mw" data-category="Q&#x26;A" data-category-id="DIC_kwDOID2-M84CS5wo" data-mapping="pathname" data-strict="0" data-reactions-enabled="1" data-emit-metadata="0" data-input-position="bottom" data-theme="dark" data-lang="zh-CN" crossorigin="anonymous" async></script><div class="giscus"></div></div><footer class="footer-wrap"><footer class="max-container">© 2022 <a href="https://wangchujiang.com/#/app" target="_blank">Kenny Wang</a>.</footer></footer><script src="../data.js?v=1.5.5" defer></script><script src="../js/fuse.min.js?v=1.5.5" defer></script><script src="../js/main.js?v=1.5.5" defer></script><div id="mysearch"><div class="mysearch-box"><div class="mysearch-input"><div><svg xmlns="http://www.w3.org/2000/svg" height="1em" width="1em" viewBox="0 0 18 18">
  <path fill="currentColor" d="M17.71,16.29 L14.31,12.9 C15.4069846,11.5024547 16.0022094,9.77665502 16,8 C16,3.581722 12.418278,0 8,0 C3.581722,0 0,3.581722 0,8 C0,12.418278 3.581722,16 8,16 C9.77665502,16.0022094 11.5024547,15.4069846 12.9,14.31 L16.29,17.71 C16.4777666,17.8993127 16.7333625,18.0057983 17,18.0057983 C17.2666375,18.0057983 17.5222334,17.8993127 17.71,17.71 C17.8993127,17.5222334 18.0057983,17.2666375 18.0057983,17 C18.0057983,16.7333625 17.8993127,16.4777666 17.71,16.29 Z M2,8 C2,4.6862915 4.6862915,2 8,2 C11.3137085,2 14,4.6862915 14,8 C14,11.3137085 11.3137085,14 8,14 C4.6862915,14 2,11.3137085 2,8 Z"></path>
</svg><input id="mysearch-input" type="search" placeholder="搜索" autocomplete="off"><div class="mysearch-clear"></div></div><button id="mysearch-close" type="button">搜索</button></div><div class="mysearch-result"><div id="mysearch-menu"></div><div id="mysearch-content"></div></div></div></div></body>
</html>
